# -*- coding: utf-8 -*-

from django.contrib import admin

# Register your models here.
from django.forms import Widget
from django.forms.utils import flatatt
from django.forms.widgets import Input
from django.utils import formats
from django.utils.encoding import force_text
from django.utils.html import conditional_escape, format_html, html_safe
from bulls.models import Article, Banner, Picture,Category

class PicUploader(Widget):
    class Media:
        js=('admin/simple-Uploader/simple-uploader.js',)
        css = {'all':('admin/simple-Uploader/simple-uploader.css',)}

    def __init__(self,post_url='',size_limit=2048,type_limit='gif|jpg|jpeg|png',attrs=None):
        if post_url:
            self.post_url = post_url
        if size_limit:
            self.size_limit = size_limit
        if type_limit:
            self.type_limit = type_limit
        super(PicUploader,self).__init__(attrs)

    post_url=''
    size_limit=1024
    type_limit='gif'

    def _format_value(self, value):
        if self.is_localized:
            return formats.localize_input(value)
        return value

    def render(self, name, value, attrs=None):
        if value is None:
            value = ''

        final_attrs = self.build_attrs(attrs, name=name,post_url=self.post_url,size_limit=self.size_limit,type_limit=self.type_limit)

        if value != '':
            # Only add the 'value' attribute if a value is non-empty.
            final_attrs['value'] = force_text(self._format_value(value))

        return  '''
            <input type="hidden" id="%s"/>
            <div style="position:relative;left:106px; width:206px; height: 20px;padding:2px 3px;margin:2px 0;" id="%s_holder">
            </div>
            <script type="text/javascript">
                aa = simple.uploader.create({
                        appendTo:document.getElementById("%s_holder"),
                        url:"%s",
                        valueBindTo:document.getElementById("%s"),
                        limit:{
                            fileFormat:"%s",
                            fileSizeMax:%s
                        },
                        uiText:{
                            selectFile:"选择文件！",
                            limit:"文件错误"
                        }
                    });
            </script>
        '''% (final_attrs['id'],final_attrs['id'],final_attrs['id'],final_attrs['post_url'],final_attrs['id'],self.type_limit,self.size_limit)
        # return  format_html('<input{} 11/>', flatatt(final_attrs))


class PictureInline(admin.TabularInline):
    model = Picture
    extra = 1


class ArticleAdmin(admin.ModelAdmin):
    inlines = [PictureInline]

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'media':
            return db_field.formfield(widget = PicUploader(post_url='/bulls/upload/',size_limit=512))
        return super(ArticleAdmin, self).formfield_for_dbfield(db_field, **kwargs)


class CategoryAdmin(admin.ModelAdmin):
    search_fields = ['name']
    list_display = ['name','parent']


admin.site.register(Article,ArticleAdmin)

admin.site.register(Banner)

admin.site.register(Category,CategoryAdmin)
